const Promise = require('bluebird');

module.exports = function ({PermissionsRoles, sequelize}) {

    /**
     * 保存权限 V2
     */
    PermissionsRoles.saveV2 = function (body) {
        return sequelize.transaction(function (t) {
            let {role, ids} = body;
            return PermissionsRoles.destroy({where: {role: role}, transaction: t}).then(function () {
                let permissionsRoles = [];
                for (let i in ids) {
                    permissionsRoles.push({role: role, permission_id: ids[i]});
                }
                return PermissionsRoles.bulkCreate(permissionsRoles, {transaction: t}).then(function () {
                    return {success: true};
                });
            });
        });
    }

}